package com.dkm.ec.sale.domain;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.api.service.IdGenerator;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.ec.sale.persistence.dao.SalePackageItemDao;
import com.dkm.ec.sale.persistence.entity.SaleEntityPo;
import com.dkm.ec.sale.persistence.entity.SalePackageItemPo;
import com.dkm.ec.sale.repository.SaleEntityRepository;
import com.dkm.ec.sale.repository.SalePackageItemRepository;
import com.dkm.ec.sale.repository.SaleSkuRepository;

/**
 * 对象功能:产品套餐构件 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:YuanJiaQiu
 * 创建时间:2015-08-13 10:52:54
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class SalePackageItem extends AbstractDomain<String, SalePackageItemPo>{
	
	@Resource
	private SaleSkuRepository saleSkuRepository;
	@Resource
	private IdGenerator idGenerator;
	@Resource
	private SalePackageItemRepository salePackageItemRepository;
	@Resource
	private SaleEntityRepository saleEntityRepository ;
	
	private SalePackageItemDao salePackageItemDao = null;

	protected void init(){
		salePackageItemDao = SpringHelper.getBean(SalePackageItemDao.class);
		setDao(salePackageItemDao);
	}	 
	
	public void save(String packageSkuId, String[] skuIdArr){
		SaleSku packageSku = saleSkuRepository.load(packageSkuId);
		if(packageSku != null && skuIdArr != null && skuIdArr.length > 0){
			for(int i=0; i<skuIdArr.length; i++){
				//防止重复加入
				List<SalePackageItemPo> salePackageItemPos = salePackageItemRepository.findBySkuIdAndItemSkuId(packageSkuId, skuIdArr[i]);
				if(salePackageItemPos != null && salePackageItemPos.size() > 0){
					continue;
				}
				SaleSku saleSku = saleSkuRepository.load(skuIdArr[i]);
				if(saleSku != null){
					SaleEntity saleEntity =saleEntityRepository.load(saleSku.getData().getSaleProdId());
					SaleEntityPo saleEntityPo = saleEntity.getData();
					if(saleEntityPo.getSeType().equals("package")){ //添加套餐项的类型不能为套餐
						continue;
					}
					SalePackageItemPo salePackageItemPo = new SalePackageItemPo();
					salePackageItemPo.setId(idGenerator.genSid());
					salePackageItemPo.setProdId(packageSku.getData().getSaleProdId());
					salePackageItemPo.setSkuId(packageSku.getId());
					salePackageItemPo.setItemProdId(saleSku.getData().getSaleProdId());
					salePackageItemPo.setItemSkuId(saleSku.getData().getId());
					salePackageItemPo.setItemCount(1);
					salePackageItemPo.setSort(1);
					salePackageItemDao.create(salePackageItemPo);
				}
			}
		}
	}
	
	
	
	public void  editPackage(Map<String, Object> dataMap){}
		
}
